<!--
 * @FileDescription PDF查看器
 * @Author 杨宇翔
 * @Date 20220727 15:40:50
 * @LastEditors 杨宇翔
 * @LastEditTime 20220727 15:40:50
-->
<template>
    <!-- <iframe :name="props.Name" ref="viewer"
        :src="`/static/pdfjs/web/viewer.html?file=${props.Url ?? PDFViewerComponent.Default.Src.value}`"></iframe> -->
</template>

<script setup lang="ts">
import PDFViewerComponent from '@runtime/module/PDFViewer/PDFViewerComponent';
import { onBeforeUnmount, onMounted, onUnmounted, ref, Ref } from 'vue';

interface Props {
    Name: string;
    Url?: string;
    OnLoad?: Function;
}

const props = defineProps<Props>();
const viewer: Ref<HTMLIFrameElement | null> = ref(null);


function OnViewerLoad(): void {
    PDFViewerComponent.Default.AddViewer(props.Name, viewer.value!);
    if (props.OnLoad != undefined) {
        props.OnLoad();
    }
    viewer.value?.removeEventListener('load', OnViewerLoad);
}

function OnViewerMounted(): void {
    viewer.value?.addEventListener('load', OnViewerLoad);
}

function OnViewerUnmounted(): void {
    viewer.value?.removeEventListener('load', OnViewerLoad);
}

onMounted(OnViewerMounted);
onBeforeUnmount(() => PDFViewerComponent.Default.RemoveViewer(props.Name));
onUnmounted(OnViewerUnmounted);
</script>

<style scoped>
</style>